package chuangyi.com.org.DOMIHome.adapter;

import android.view.View;
import android.view.ViewGroup;

import com.daimajia.androidanimations.library.Techniques;
import com.daimajia.androidanimations.library.YoYo;
import com.daimajia.swipe.SimpleSwipeListener;
import com.daimajia.swipe.SwipeLayout;
import com.daimajia.swipe.adapters.BaseSwipeAdapter;

/**
 * Created by zxh on 16/12/25.
 */


import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableList;
import android.databinding.ViewDataBinding;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import chuangyi.com.org.DOMIHome.R;
import me.maxwin.view.XListView;

/**
 * Created by zxh on 16/11/16.
 */
public class SwipeListAdapter extends BaseSwipeAdapter
        implements XListAdapterInterface {

    public static final int ACTION_ADD = 1;
    public static final int ACTION_DEL = 2;

    private List<BindingListItem> added;
    private List<BindingListItem> removed;
    private List<BindingListItem> items;

    private Context mContext;
    private int variableId;
    private HashMap<Integer, Integer> typeMap;

    private XListView xListView;
    private XListView.IXListViewListener xListViewListener;

    public XListView.IXListViewListener getxListViewListener() {
        return xListViewListener;
    }

    public void setxListViewListener(XListView.IXListViewListener xListViewListener) {
        this.xListViewListener = xListViewListener;
    }

    public XListView getxListView() {
        return xListView;
    }

    public void setxListView(XListView xListView) {
        this.xListView = xListView;
    }

    public Context getContext() {
        return mContext;
    }

    public void setVariableId(int variableId) {
        this.variableId = variableId;
    }

    public void setViewType(List<Integer> viewTypeList) {
        int typeId = 0;
        for (Integer viewType : viewTypeList) {
            typeMap.put(viewType, typeId);
            typeId++;
        }
        notifyDataSetInvalidated();
    }

    public SwipeListAdapter(Context context, int variableId) {
        this(context, variableId, null);
    }

    /**
     * 新建绑定列表适配器
     *
     * @param context
     * @param variableId
     */
    public SwipeListAdapter(Context context, int variableId, List<Integer> viewTypeList) {
        items = new ArrayList<BindingListItem>();
        this.mContext = context;
        this.variableId = variableId;
        this.typeMap = new HashMap<Integer, Integer>();
        if (viewTypeList != null) {
            setViewType(viewTypeList);
        }
        removed = new ArrayList<BindingListItem>();
        added = new ArrayList<BindingListItem>();
    }

    @Override
    public void add(BindingListItem item) {
        synchronized (added) {
            added.add(item);
        }
    }

    @Override
    public void remove(BindingListItem item) {
        synchronized (added) {
            removed.add(item);
        }
    }

    @Override
    public int getViewTypeCount() {
        return typeMap.size() == 0 ? 1 : typeMap.size();
    }

    @Override
    public int getItemViewType(int position) {
        return typeMap.size() == 0 ? 0 : typeMap.get(items.get(position).getLayout());
    }

    @Override
    public void notifyDataSetChanged() {
        synchronized (added) {
            for (BindingListItem item : added) {
                items.add(item);
            }
            for (BindingListItem item : removed) {
                items.remove(item);
            }
            added.clear();
            removed.clear();
        }
        super.notifyDataSetChanged();
    }

    @Override
    public void clear(boolean notify) {
        synchronized (added) {
            items.clear();
            if (notify) {
                closeAllItems();
                super.notifyDataSetChanged();
            }
        }
    }

    @Override
    public int getSwipeLayoutResourceId(int position) {
        return 0;
//        return R.id.swipe;
    }

    @Override
    public View generateView(int position, ViewGroup parent) {
        ViewDataBinding dataBinding = null;
        final BindingListItem item = items.get(position);
        dataBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), item.getLayout(), parent, false);

        dataBinding.setVariable(variableId, item);
        View root = dataBinding.getRoot();
//        SwipeLayout swipeLayout = (SwipeLayout) root.findViewById(R.id.swipe);
//        swipeLayout.setShowMode(SwipeLayout.ShowMode.PullOut);
        return root;
    }

    @Override
    public void fillValues(int position, View convertView) {
        BindingListItem item = items.get(position);
        ViewDataBinding dataBinding = DataBindingUtil.findBinding(convertView);
        if (dataBinding != null) {
            dataBinding.setVariable(variableId, item);
        }
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }
}
